home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 13 / MacFormat n. 13 (Spain) / Macformat 13.bin / Shareware Internet / Desarrolladores / ICAppSourceKit1.2 / ICIconSuites.p < prev    next >
Encoding:
Text File  |  1995-11-07  |  5.4 KB  |  220 lines

  1. unit ICIconSuites;
  2.  
  3. interface
  4.  
  5.     uses
  6.         Types;
  7.         
  8.     procedure InitICIconSuites;
  9.     function GetDTDBIconSuiteCached (ftype, fcreator: OSType; var suite: Handle): OSErr;
  10.  
  11. implementation
  12.  
  13.     uses
  14.         Files, Errors, ICGlobals, ICMiscSubs, Icons;
  15.  
  16.     function GetDTDBIcon (ftype, fcreator: OSType; var cookie: integer; icon_to_get: SignedByte; var pbdt: DTPBRec): OSErr;
  17.         function GetTheIcon (dtrn: integer): boolean;
  18.         begin
  19.             pbdt.ioDTRefNum := dtrn;
  20.             pbdt.ioTagInfo := 0;
  21.             pbdt.ioIconType := icon_to_get;
  22.             pbdt.ioFileCreator := fcreator;
  23.             pbdt.ioFileType := ftype;
  24.             GetTheIcon := PBDTGetIconSync(@pbdt) = noErr;
  25.         end;
  26.  
  27.         var
  28.             oe: OSErr;
  29.             i: integer;
  30.             found: boolean;
  31.             junkstr: Str63;
  32.             vrefnum: OSErr;
  33.             crdate: longint;
  34.     begin
  35.         found := false;
  36.         if system7 then begin
  37.             if cookie = 0 then begin
  38.                 i := 1;
  39.                 repeat
  40.                     vrefnum := 0;
  41.                     junkstr := '';
  42.                     oe := GetVolInfo(junkstr, vrefnum, i, crdate);
  43.                     i := i + 1;
  44.                     if oe = noErr then begin
  45.                         with pbdt do begin
  46.                             ioNamePtr := nil;
  47.                             ioVRefNum := vrefnum;
  48.                             oe := PBDTGetPath(@pbdt);
  49.                             if oe = noErr then begin
  50.                                 if GetTheIcon(pbdt.ioDTRefNum) then begin
  51.                                     cookie := pbdt.ioDTRefNum;
  52.                                     found := true;
  53.                                 end;
  54.                             end;
  55.                         end;
  56.                         oe := noErr;
  57.                     end;
  58.                 until found or (oe <> noErr);
  59.             end else begin
  60.                 found := GetTheIcon(cookie);
  61.             end;
  62.         end;
  63.         if found then begin
  64.             oe := noErr;
  65.         end else begin
  66.             oe := afpItemNotFound;
  67.         end;
  68.         GetDTDBIcon := oe;
  69.     end; (* GetDTDBIcon *)
  70.  
  71.     var
  72.         icon_buffer: packed array[0..1023] of byte;
  73.  
  74.     function GetDTDBIconH (ftype, fcreator: OSType; var cookie: integer; icon_to_get: SignedByte; var iconh: Handle): OSErr;
  75.         var
  76.             dtpb: DTPBRec;
  77.             err: OSErr;
  78.     begin
  79.         iconh := nil;
  80.         dtpb.ioDTBuffer := @icon_buffer;
  81.         dtpb.ioDTReqCount := sizeof(icon_buffer);
  82.         err := GetDTDBIcon(ftype, fcreator, cookie, icon_to_get, dtpb);
  83.         if err = noErr then begin
  84.             err := PtrToHand(@icon_buffer, iconh, dtpb.ioDTActCount);
  85.         end; (* if *)
  86.         if err <> noErr then begin
  87.             DisposeHandle(iconh);
  88.             iconh := nil;
  89.         end; (* if *)
  90.         GetDTDBIconH := err;
  91.     end; (* GetDTDBIconH *)
  92.  
  93.     function GetDTDBAddSuite (suite: Handle; ftype, fcreator: OSType; var cookie: integer; icon_to_get: SignedByte; icon_to_put: OSType): OSErr;
  94.         var
  95.             err: OSErr;
  96.             iconh: Handle;
  97.     begin
  98.         err := GetDTDBIconH(ftype, fcreator, cookie, icon_to_get, iconh);
  99.         if err = noErr then begin
  100.             err := AddIconToSuite(iconh, suite, icon_to_put);
  101.         end; (* if *)
  102.         if err <> noErr then begin
  103.             DisposeHandle(iconh);
  104.             iconh := nil;
  105.         end; (* if *)
  106.         GetDTDBAddSuite := err;
  107.     end; (* GetDTDBAddSuite *)
  108.  
  109.     function GetDTDBIconSuiteUncached (ftype, fcreator: OSType; var suite: Handle): OSErr;
  110.         var
  111.             err: OSErr;
  112.             junk: OSErr;
  113.             cookie: integer;
  114.     begin
  115.         suite := nil;
  116.         err := NewIconSuite(suite);
  117.         if err = noErr then begin
  118.             cookie := 0;
  119.             if GetDTDBAddSuite(suite, ftype, fcreator, cookie, kLargeIcon, large1BitMask) = noErr then begin
  120.                 junk := GetDTDBAddSuite(suite, ftype, fcreator, cookie, kLarge4BitIcon, large4BitData);
  121.                 junk := GetDTDBAddSuite(suite, ftype, fcreator, cookie, kLarge8BitIcon, large8BitData);
  122.             end else begin
  123.                 err := afpItemNotFound;
  124.             end; (* if *)
  125.         end; (* if *)
  126.         if err <> noErr then begin
  127.             if suite <> nil then begin
  128.                 junk := DisposeIconSuite(suite, true);
  129.                 suite := nil;
  130.             end; (* if *)
  131.         end; (* if *)
  132.         GetDTDBIconSuiteUncached := err;
  133.     end; (* GetDTDBIconSuiteUncached *)
  134.  
  135.     const
  136.         cache_max = 20;
  137.  
  138.     type
  139.         CacheRecord = record
  140.                 usage: longInt;
  141.                 ftype, fcreator: OSType;
  142.                 suite: handle;
  143.             end;
  144.  
  145.     var
  146.         cache: array[1..cache_max] of CacheRecord;
  147.         usage: longInt;
  148.         default_application_suite: handle;
  149.         default_document_suite: handle;
  150.  
  151.     function GetDTDBIconSuiteCached (ftype, fcreator: OSType; var suite: Handle): OSErr;
  152.         var
  153.             err, junk: OSErr;
  154.             i, j: integer;
  155.             m: longInt;
  156.     begin
  157.         err := -1;
  158.         suite := nil;
  159.         for i := 1 to cache_max do begin
  160.             if (cache[i].usage > 0) & (cache[i].ftype = ftype) & (cache[i].fcreator = fcreator) then begin
  161.                 suite := cache[i].suite;
  162.                 err := noErr;
  163.                 cache[i].usage := usage;
  164.                 usage := usage + 1;
  165.                 leave;
  166.             end;
  167.         end;
  168.         if err <> noErr then begin
  169.             m := maxLongInt;
  170.             for i := 1 to cache_max do begin
  171.                 if (cache[i].usage < m) then begin
  172.                     j := i;
  173.                     m := cache[i].usage;
  174.                 end;
  175.             end;
  176.             err := GetDTDBIconSuiteUncached(ftype, fcreator, suite);
  177.             if err = noErr then begin
  178.                 if m > 0 then begin
  179.                     junk := DisposeIconSuite(cache[j].suite, true);
  180.                 end;
  181.                 cache[j].suite := suite;
  182.                 cache[j].ftype := ftype;
  183.                 cache[j].fcreator := fcreator;
  184.                 cache[j].usage := usage;
  185.                 usage := usage + 1;
  186.             end;
  187.         end;
  188.         if (err = noErr) & (suite = nil) then begin
  189.             err := resNotFound;
  190.         end;
  191.         if (err <> noErr) then begin
  192.             suite := default_document_suite;
  193.             if (ftype = 'APPL') & (default_application_suite <> nil) then begin
  194.                 suite := default_application_suite;
  195.             end;
  196.         end;
  197.         GetDTDBIconSuiteCached := err;
  198.     end;
  199.  
  200.     procedure InitICIconSuites;
  201.         var
  202.             i: integer;
  203.     begin
  204.         for i := 1 to cache_max do begin
  205.             cache[i].usage := -1;
  206.         end;
  207.         usage := 1;
  208.         default_application_suite := nil;
  209.         default_document_suite := nil;
  210.         if system7 then begin
  211.             if GetIconSuite(default_document_suite, -4000, svAllLargeData) <> noErr then begin
  212.                 default_document_suite := nil;
  213.             end; (* if *)
  214.             if GetIconSuite(default_application_suite, -3996, svAllLargeData) <> noErr then begin
  215.                 default_application_suite := nil;
  216.             end; (* if *)
  217.         end; (* if *)
  218.     end;
  219.  
  220. end.